//
//  CS_Pop_ToastView.m
//  Pods
//
//  Created by 网新中研 on 17/3/1.
//
//

#import "CS_Pop_ToastView.h"

#define HEIGHT_TOAST_VIEW 35.0

@interface CS_Pop_ToastView()

@property (nonatomic, strong) UIView *informView;

@property (nonatomic, strong) UIView *superView;

@property (nonatomic, strong) UILabel *labelView;

@end

@implementation CS_Pop_ToastView

static CS_Pop_ToastView *_toastView;

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.labelView.frame = CGRectMake(10, 0, frame.size.width - 20, frame.size.height);
    }
    return self;
}

+(instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _toastView = [CS_Pop_ToastView new];
        [_toastView addSubview:_toastView.labelView];
        
    });
    return _toastView;
}

+(void)showWithMessage:(NSString *)message superView:(UIView *)superView;{
    [CS_Pop_ToastView showWithMessage:message toastType:CSToastTypeNormal superView:superView];
}

+(void)showWithMessage:(NSString *)message toastType:(ToastType)toastType superView:(UIView *)superView{

    CS_Pop_ToastView *toastView = [CS_Pop_ToastView sharedInstance];
    [toastView removeFromSuperview];
    toastView.backgroundColor = COLOR_THEME;
    toastView.frame = CGRectMake(0, 0, superView.frame.size.width, HEIGHT_TOAST_VIEW);
    toastView.labelView.frame = CGRectMake(20, 0, toastView.frame.size.width - 40, toastView.frame.size.height);
    toastView.labelView.text = message;
    toastView.superView = superView;
    
    switch (toastType) {
        case CSToastTypeBottom:
        {
            [toastView bottomSlideShow];
        }
            break;
        case CSToastTypeTop:
        {
            [toastView topSlideShow];
        }
            break;
        default:
            break;
    }
    
}

-(void)topSlideShow{
    [_superView addSubview:self];
    WS(weakSelf);
    self.frame = CGRectMake(0, -HEIGHT_TOAST_VIEW, _superView.frame.size.width, HEIGHT_TOAST_VIEW);
//    self.layer.opacity = 0.97;
//    self.layer.cornerRadius = 5.0;
//    self.layer.masksToBounds = YES;
    
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        weakSelf.frame = CGRectMake(0, 20, _superView.frame.size.width, HEIGHT_TOAST_VIEW);
        
    } completion:^(BOOL finished) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [weakSelf hideTopToastView];
            
        });
    }];
}

-(void)bottomSlideShow{
    [_superView addSubview:self];
    WS(weakSelf);
    self.frame = CGRectMake(0, kWindowH, _superView.frame.size.width, HEIGHT_TOAST_VIEW);
    
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        weakSelf.frame = CGRectMake(0, kWindowH - HEIGHT_TOAST_VIEW, _superView.frame.size.width, HEIGHT_TOAST_VIEW);
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf hideBottomToastView];
        });
    }];
}

-(void)hideTopToastView{
    WS(weakSelf);
    [UIView animateWithDuration:0.4 animations:^{
        weakSelf.layer.opacity = 0.5;
        weakSelf.frame = CGRectMake(0, -HEIGHT_TOAST_VIEW, weakSelf.superView.frame.size.width, HEIGHT_TOAST_VIEW);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
        weakSelf.layer.opacity = 1.0;
    }];
}


-(void)hideBottomToastView{
    WS(weakSelf);
    [UIView animateWithDuration:0.4 animations:^{
        weakSelf.layer.opacity = 0.5;
        weakSelf.frame = CGRectMake(0, kWindowH, weakSelf.superView.frame.size.width, HEIGHT_TOAST_VIEW);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
        weakSelf.layer.opacity = 1.0;
    }];
}

-(UILabel *)labelView{
    if (_labelView == nil) {
        _labelView = [[UILabel alloc]init];
        _labelView.textColor = [UIColor whiteColor];
        _labelView.font = [UIFont systemFontOfSize:16.0];
        _labelView.textAlignment = NSTextAlignmentCenter;
    }
    return _labelView;
}


@end
